<!DOCTYPE html>

<html>
  <head>
    <meta charset="utf-8">
    
    <title>numpy.testing.suppress_warnings &mdash; NumPy v1.18 Manual</title>
    
    <link rel="stylesheet" type="text/css" href="../../_static/css/spc-bootstrap.css">
    <link rel="stylesheet" type="text/css" href="../../_static/css/spc-extend.css">
    <link rel="stylesheet" href="../../_static/scipy.css" type="text/css" >
    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" >
    <link rel="stylesheet" href="../../_static/graphviz.css" type="text/css" >
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../',
        VERSION:     '1.18.1',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  false
      };
    </script>
    <script type="text/javascript" src="../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../_static/doctools.js"></script>
    <script type="text/javascript" src="../../_static/language_data.js"></script>
    <script type="text/javascript" src="../../_static/js/copybutton.js"></script>
    <link rel="author" title="About these documents" href="../../about.html" >
    <link rel="index" title="Index" href="../../genindex.html" >
    <link rel="search" title="Search" href="../../search.html" >
    <link rel="top" title="NumPy v1.18 Manual" href="../../index.html" >
    <link rel="up" title="Test Support (numpy.testing)" href="../routines.testing.html" >
    <link rel="next" title="numpy.testing.suppress_warnings.__call__" href="numpy.testing.suppress_warnings.__call__.html" >
    <link rel="prev" title="numpy.testing.rundocs" href="numpy.testing.rundocs.html" > 
  </head>
  <body>
<div class="container">
  <div class="top-scipy-org-logo-header" style="background-color: #a2bae8;">
    <a href="../../index.html">
      <img border=0 alt="NumPy" src="../../_static/numpy_logo.png"></a>
    </div>
  </div>
</div>


    <div class="container">
      <div class="main">
        
	<div class="row-fluid">
	  <div class="span12">
	    <div class="spc-navbar">
              
    <ul class="nav nav-pills pull-left">
        <li class="active"><a href="https://numpy.org/">NumPy.org</a></li>
        <li class="active"><a href="https://numpy.org/doc">Docs</a></li>
        
        <li class="active"><a href="../../index.html">NumPy v1.18 Manual</a></li>
        

          <li class="active"><a href="../index.html" >NumPy Reference</a></li>
          <li class="active"><a href="../routines.html" >Routines</a></li>
          <li class="active"><a href="../routines.testing.html" accesskey="U">Test Support (<code class="xref py py-mod docutils literal notranslate"><span class="pre">numpy.testing</span></code>)</a></li> 
    </ul>
              
              
    <ul class="nav nav-pills pull-right">
      <li class="active">
        <a href="../../genindex.html" title="General Index"
           accesskey="I">index</a>
      </li>
      <li class="active">
        <a href="numpy.testing.suppress_warnings.__call__.html" title="numpy.testing.suppress_warnings.__call__"
           accesskey="N">next</a>
      </li>
      <li class="active">
        <a href="numpy.testing.rundocs.html" title="numpy.testing.rundocs"
           accesskey="P">previous</a>
      </li>
    </ul>
              
	    </div>
	  </div>
	</div>
        

	<div class="row-fluid">
      <div class="spc-rightsidebar span3">
        <div class="sphinxsidebarwrapper">
  <h4>Previous topic</h4>
  <p class="topless"><a href="numpy.testing.rundocs.html"
                        title="previous chapter">numpy.testing.rundocs</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="numpy.testing.suppress_warnings.__call__.html"
                        title="next chapter">numpy.testing.suppress_warnings.__call__</a></p>
<div id="searchbox" style="display: none" role="search">
  <h4>Quick search</h4>
    <div>
    <form class="search" action="../../search.html" method="get">
      <input type="text" style="width: inherit;" name="q" />
      <input type="submit" value="search" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
          <div class="span9">
            
        <div class="bodywrapper">
          <div class="body" id="spc-section-body">
            
  <div class="section" id="numpy-testing-suppress-warnings">
<h1>numpy.testing.suppress_warnings<a class="headerlink" href="#numpy-testing-suppress-warnings" title="Permalink to this headline">¶</a></h1>
<dl class="class">
<dt id="numpy.testing.suppress_warnings">
<em class="property">class </em><code class="sig-prename descclassname">numpy.testing.</code><code class="sig-name descname">suppress_warnings</code><span class="sig-paren">(</span><em class="sig-param">forwarding_rule='always'</em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/numpy/numpy/blob/v1.18.1/numpy/testing/_private/utils.py#L2005-L2281"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#numpy.testing.suppress_warnings" title="Permalink to this definition">¶</a></dt>
<dd><p>Context manager and decorator doing much the same as
<code class="docutils literal notranslate"><span class="pre">warnings.catch_warnings</span></code>.</p>
<p>However, it also provides a filter mechanism to work around
<a class="reference external" href="https://bugs.python.org/issue4180">https://bugs.python.org/issue4180</a>.</p>
<p>This bug causes Python before 3.4 to not reliably show warnings again
after they have been ignored once (even within catch_warnings). It
means that no “ignore” filter can be used easily, since following
tests might need to see the warning. Additionally it allows easier
specificity for testing warnings and can be nested.</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><dl class="simple">
<dt><strong>forwarding_rule</strong><span class="classifier">str, optional</span></dt><dd><p>One of “always”, “once”, “module”, or “location”. Analogous to
the usual warnings module filter mode, it is useful to reduce
noise mostly on the outmost level. Unsuppressed and unrecorded
warnings will be forwarded based on this rule. Defaults to “always”.
“location” is equivalent to the warnings “default”, match by exact
location the warning warning originated from.</p>
</dd>
</dl>
</dd>
</dl>
<p class="rubric">Notes</p>
<p>Filters added inside the context manager will be discarded again
when leaving it. Upon entering all filters defined outside a
context will be applied automatically.</p>
<p>When a recording filter is added, matching warnings are stored in the
<code class="docutils literal notranslate"><span class="pre">log</span></code> attribute as well as in the list returned by <code class="docutils literal notranslate"><span class="pre">record</span></code>.</p>
<p>If filters are added and the <code class="docutils literal notranslate"><span class="pre">module</span></code> keyword is given, the
warning registry of this module will additionally be cleared when
applying it, entering the context, or exiting it. This could cause
warnings to appear a second time after leaving the context if they
were configured to be printed once (default) and were already
printed before the context was entered.</p>
<p>Nesting this context manager will work as expected when the
forwarding rule is “always” (default). Unfiltered and unrecorded
warnings will be passed out and be matched by the outer level.
On the outmost level they will be printed (or caught by another
warnings context). The forwarding rule argument can modify this
behaviour.</p>
<p>Like <code class="docutils literal notranslate"><span class="pre">catch_warnings</span></code> this context manager is not threadsafe.</p>
<p class="rubric">Examples</p>
<p>With a context manager:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">with</span> <span class="n">np</span><span class="o">.</span><span class="n">testing</span><span class="o">.</span><span class="n">suppress_warnings</span><span class="p">()</span> <span class="k">as</span> <span class="n">sup</span><span class="p">:</span>
    <span class="n">sup</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="ne">DeprecationWarning</span><span class="p">,</span> <span class="s2">&quot;Some text&quot;</span><span class="p">)</span>
    <span class="n">sup</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">module</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">ma</span><span class="o">.</span><span class="n">core</span><span class="p">)</span>
    <span class="n">log</span> <span class="o">=</span> <span class="n">sup</span><span class="o">.</span><span class="n">record</span><span class="p">(</span><span class="ne">FutureWarning</span><span class="p">,</span> <span class="s2">&quot;Does this occur?&quot;</span><span class="p">)</span>
    <span class="n">command_giving_warnings</span><span class="p">()</span>
    <span class="c1"># The FutureWarning was given once, the filtered warnings were</span>
    <span class="c1"># ignored. All other warnings abide outside settings (may be</span>
    <span class="c1"># printed/error)</span>
    <span class="n">assert_</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">log</span><span class="p">)</span> <span class="o">==</span> <span class="mi">1</span><span class="p">)</span>
    <span class="n">assert_</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">sup</span><span class="o">.</span><span class="n">log</span><span class="p">)</span> <span class="o">==</span> <span class="mi">1</span><span class="p">)</span>  <span class="c1"># also stored in log attribute</span>
</pre></div>
</div>
<p>Or as a decorator:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">sup</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">testing</span><span class="o">.</span><span class="n">suppress_warnings</span><span class="p">()</span>
<span class="n">sup</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="n">module</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">ma</span><span class="o">.</span><span class="n">core</span><span class="p">)</span>  <span class="c1"># module must match exactly</span>
<span class="nd">@sup</span>
<span class="k">def</span> <span class="nf">some_function</span><span class="p">():</span>
    <span class="c1"># do something which causes a warning in np.ma.core</span>
    <span class="k">pass</span>
</pre></div>
</div>
<p class="rubric">Methods</p>
<table class="longtable docutils align-default">
<colgroup>
<col style="width: 10%" />
<col style="width: 90%" />
</colgroup>
<tbody>
<tr class="row-odd"><td><p><a class="reference internal" href="numpy.testing.suppress_warnings.__call__.html#numpy.testing.suppress_warnings.__call__" title="numpy.testing.suppress_warnings.__call__"><code class="xref py py-obj docutils literal notranslate"><span class="pre">__call__</span></code></a>(self, func)</p></td>
<td><p>Function decorator to apply certain suppressions to a whole function.</p></td>
</tr>
<tr class="row-even"><td><p><a class="reference internal" href="numpy.testing.suppress_warnings.filter.html#numpy.testing.suppress_warnings.filter" title="numpy.testing.suppress_warnings.filter"><code class="xref py py-obj docutils literal notranslate"><span class="pre">filter</span></code></a>(self[, category, message, module])</p></td>
<td><p>Add a new suppressing filter or apply it if the state is entered.</p></td>
</tr>
<tr class="row-odd"><td><p><a class="reference internal" href="numpy.testing.suppress_warnings.record.html#numpy.testing.suppress_warnings.record" title="numpy.testing.suppress_warnings.record"><code class="xref py py-obj docutils literal notranslate"><span class="pre">record</span></code></a>(self[, category, message, module])</p></td>
<td><p>Append a new recording filter or apply it if the state is entered.</p></td>
</tr>
</tbody>
</table>
</dd></dl>

</div>


          </div>
        </div>
          </div>
        </div>
      </div>
    </div>

    <div class="container container-navbar-bottom">
      <div class="spc-navbar">
        
      </div>
    </div>
    <div class="container">
    <div class="footer">
    <div class="row-fluid">
    <ul class="inline pull-left">
      <li>
        &copy; Copyright 2008-2019, The SciPy community.
      </li>
      <li>
      Last updated on Feb 20, 2020.
      </li>
      <li>
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 2.4.2.
      </li>
    </ul>
    </div>
    </div>
    </div>
  </body>
</html>